package com.fbt.energy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbt.energy.dao.SparePartDao;
import com.fbt.energy.entity.SparePart;
import com.fbt.energy.service.SparePartService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 备件_清单表(SparePart)表服务实现类
 *
 * @author makejava
 * @since 2022-01-21 15:22:11
 */
@Service("sparePartService")
public class SparePartServiceImpl extends ServiceImpl<SparePartDao,SparePart> implements SparePartService {
    @Resource
    private SparePartDao sparePartDao;

    @Resource
    private  SparePartService sparePartService;

    /**
     * 通过ID查询单条数据
     *
     * @param spId 主键
     * @return 实例对象
     */
    @Override
    public SparePart queryById(Integer spId) {
        return this.sparePartDao.queryById(spId);
    }

    /**
     * 分页查询
     *
     * @param sparePart 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<SparePart> queryByPage(SparePart sparePart, PageRequest pageRequest) {
        long total = this.sparePartDao.count(sparePart);
        return new PageImpl<>(this.sparePartDao.queryAllByLimit(sparePart, pageRequest), pageRequest, total);
    }

    /**
     * 新增 零件

     */
    @Override
    public SparePart insert(SparePart sparePart) {
        this.sparePartDao.insert(sparePart);
        return sparePart;
    }

    /**
     * 修改数据
     *
     * @param sparePart 实例对象
     * @return 实例对象
     */
    @Override
    public SparePart update(SparePart sparePart) {
        this.sparePartDao.update(sparePart);
        return this.getSpareBySpNo(sparePart.getSpNo());
    }

    /**
     * 通过主键删除数据
     */
    @Override
    public boolean deleteById(String spNo) {
        return this.sparePartDao.deleteById(spNo) > 0;
    }


    /**根据设备编号查备件列表*/
    @Override
    public List<SparePart> getsparePars(String eqNo) {

        QueryWrapper<SparePart>sparePartQueryWrapper=new QueryWrapper<>();
        sparePartQueryWrapper.like("eq_no", eqNo);
        List<SparePart> sparePartList = sparePartService.list(sparePartQueryWrapper);


        return sparePartList;
    }

    /**查询单个零件*/
    @Override
    public SparePart getSpareBySpNo(String spNo) {

        QueryWrapper<SparePart>sparePartQueryWrappe=new QueryWrapper<>();
        sparePartQueryWrappe.eq("sp_no",spNo);
        SparePart sparePart = sparePartService.getOne(sparePartQueryWrappe);
        return sparePart;
    }

    @Override
    public SparePart ossAdd(SparePart sparePart) {

        SparePart insert = sparePartService.insert(sparePart);


        return insert;
    }

    /**根据备件编号查询备件图片*/
    @Override
    public List<String> getPictureBySpNo(String spNo) {

        QueryWrapper<SparePart>sparePartQueryWrapper=new QueryWrapper<>();
        sparePartQueryWrapper.eq("sp_no", spNo);
      //  sparePartQueryWrapper.ne("picture", null);
        List<SparePart> SpList = sparePartService.list(sparePartQueryWrapper);
        List<String> collect = SpList.stream().map(SparePart::getPicture).collect(Collectors.toList());


        return collect;
    }

    /**删除数据库中图片地址*/
    @Override
    public boolean deletePicture(String ext1) {

        QueryWrapper<SparePart> sparePartQueryWrapper=new QueryWrapper<>();
        sparePartQueryWrapper.eq("picture",ext1);
        boolean remove = sparePartService.remove(sparePartQueryWrapper);


        return remove;
    }
}
